package org.walkframework.sample.business.sample.adapter.web.request;

import lombok.Data;
import org.walkframework.boot.ehdb.bean.BaseBean;
import org.walkframework.boot.ehdb.bean.OrderBy;
import org.walkframework.boot.ehdb.bean.Pagination;
import org.walkframework.boot.util.CollectionUtil;

import java.util.List;

/**
 * 通用分页请求对象
 *
 * @author 尾生
 * @since 2024/4/9
 */
@Data
public class PageRequest {

    private int page = 1;

    private int pageSize = 10;

    private long total = -1;

    private List<OrderBy> orderBys;

    public Pagination toPagination() {
        return new Pagination(page, pageSize, total);
    }

    public void decorateOrderBys(BaseBean baseBean) {
        if (CollectionUtil.isNotEmpty(getOrderBys())) {
            // 排序需要放到最后设置，因为目前渲染是按顺序渲染的
            getOrderBys().forEach(orderBy -> baseBean.orderBy(orderBy.getName(), orderBy.isAsc()));
        }
    }
}
